home *** CD-ROM | disk | FTP | other *** search
- /*
- ***********************************************************************
- *
- * This is a simple program that, given a file name or a FSSpec,
- * launches an application-creator and has it handle the file. The net
- * result is exactly the same as if the user had double-clicked on the file.
- *
- * Synopsis
- * void open_selection(const FSSpec& file_spec)
- * void open_selection(const char * full_path_name)
- * Where the 'full_path_name' tells the full path name of the file that
- * should be "double-clicked". Though, a relative path name would do,
- * too. The 'file_spec' tells the file specification (volume refno,
- * directory ID and the file name) of the file to "double-click" on.
- *
- * The present program achieves the magic by sending an 'Open Selection'
- * event to the Finder. It is significantly based on the FinderEvents
- * stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
- *
- * OpenSelection event requires two parameters:
- * - alias of the Folder the file resides
- * - list containing an alias of the file(s) to "double-click" on
- *
- *
- ***********************************************************************
- */
-
- /* MacHeaders Included */
- #include "mymenv.h"
- #include <string.h>
- #include <AppleEvents.h>
- #include <Aliases.h>
-
-
- // Create an 'open selection' event to be sent to Finder
- static AppleEvent create_event_for_finder(void)
- {
- const AEEventClass kAEFinderEvents = 'FNDR'; // Finder event
- const AEEventID kOpenSelection = 'sope'; // OpenSelection event
- const OSType kFinderSignature = 'MACS'; // Our destination
- AppleEvent the_event;
- AEAddressDesc finder_id;
-
- do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
- &finder_id) );
-
- do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &the_event) );
-
- do_well( AEDisposeDesc(&finder_id) ); // the_event is created, finder_id can
- // be disposed of now
-
- return the_event;
- }
-
- // Add the alias of file or path name to the event
- // Only the path field is considered
- static void add_path_name(AppleEvent * the_event_ptr, const FSSpec& file_spec)
- {
- AliasHandle alias;
-
- FSSpec dir_spec;
- do_well( FSMakeFSSpec(file_spec.vRefNum,file_spec.parID,"\p",&dir_spec) );
- do_well( NewAliasMinimal(&dir_spec,&alias) );
- HLock((char **)alias);
- do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
- (**alias).aliasSize) );
-
- HUnlock((char **)alias);
- DisposHandle((char **)alias);
- }
-
-
- // Create a "selection" item in the event from the
- // full file name specified
- static void add_selection(AppleEvent * the_event_ptr, const FSSpec& file_spec)
- {
- AliasHandle alias;
- AEDescList selection_list;
-
- do_well( NewAliasMinimal(&file_spec,&alias) );
-
- do_well( AECreateList(nil, 0, FALSE, &selection_list) );
- HLock((char **)alias);
- do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias),
- (**alias).aliasSize) );
- HUnlock((char **)alias);
- DisposHandle((char **)alias);
-
- do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
- do_well( AEDeleteItem(&selection_list, 1) );
- assert( AEDisposeDesc(&selection_list) == noErr );
- }
-
-
- //-------------------------------------------------------------------------
- // Routing module
- //
-
- void open_selection(const FSSpec& file_spec)
- {
- AppleEvent the_event, reply;
-
- the_event = create_event_for_finder();
- add_path_name(&the_event,file_spec);
- add_selection(&the_event,file_spec);
-
- do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract,
- kAENormalPriority, kAEDefaultTimeout, nil, nil) );
-
- do_well( AEDisposeDesc(&the_event) );
- do_well( AEDisposeDesc(&reply) );
- }
-
- void open_selection(const char * full_path_name)
- {
- FSSpec file_spec;
-
- do_well( FSMakeFSSpec(0,0,(Pstr)full_path_name,&file_spec) );
- open_selection(file_spec);
- }
-
-